package com.iwechat.mybatis.generator.model;

import java.io.Serializable;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.iwechat.util.StringUtil;

/**
 * 模板消息实体对象
 *
 *
 */
public class TemplateMessage implements Serializable {
    private static final long serialVersionUID = 6184482933489732629L;

    /** 消息状态常量 */
    public static class Status {
        public static final int STATUS_0_NEW     = 0;
        public static final int STATUS_1_PROCESS = 1;
        public static final int STATUS_2_SUCCESS = 2;
        public static final int STATUS_3_FAILED  = 3;
    }

    /** 无禁发时段 */
    public static final String NO_FB_TIME  = "0";
    /** 有禁发时段 */
    public static final String HAS_FB_TIME = "1";
    /** 最高优先级，消息直接可发送的优先级 */
    public static final int    HIGH_PROP   = 3;

    private Long               seqnb;

    private Integer            status;

    private Integer            priority;

    private String             clientIp;

    private String             clientId;

    private String             openidId;

    /** 模板编号（非腾讯模板id） */
    private String             templateId;

    private String             sessionId;

    private String             sequence;

    private String             serviceType;

    private String             startTime;

    private String             stopTime;

    private Date               createTime;

    private Date               sendTime;

    private String             jsoncontent;

    /**
     * 根基开始发送时间和停止发送时间判断消息是否可发送
     *
     *
     * @return
     * @throws ParseException
     */
    public boolean canSend() throws ParseException {
        return StringUtil.onService(this.getStartTime(), this.getStopTime());
    }

    public Long getSeqnb() {
        return seqnb;
    }

    public void setSeqnb(Long seqnb) {
        this.seqnb = seqnb;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPriority() {
        return priority;
    }

    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp == null ? null : clientIp.trim();
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId == null ? null : clientId.trim();
    }

    public String getOpenidId() {
        return openidId;
    }

    public void setOpenidId(String openidId) {
        this.openidId = openidId == null ? null : openidId.trim();
    }

    /**
     * 获取模板编号（非腾讯模板id）
     *
     *
     * @return
     */
    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId == null ? null : templateId.trim();
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId == null ? null : sessionId.trim();
    }

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence == null ? null : sequence.trim();
    }

    /**
     * 根基开始发送时间和停止发送时间设置ServiceType
     *
     *
     */
    public void genServiceType() {
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(stopTime) || startTime.equals(stopTime)) {
            this.serviceType = NO_FB_TIME;
        } else {
            try {
                String day = DateFormatUtils.format(new Date(), "yyyyMMdd");
                String time1 = StringUtils.leftPad(startTime, 6, '0');
                String time2 = StringUtils.leftPad(stopTime, 6, '0');
                DateUtils.parseDate(day + time1, "yyyyMMddHHmmSS");
                DateUtils.parseDate(day + time2, "yyyyMMddHHmmSS");
                this.serviceType = HAS_FB_TIME;
            } catch (Exception e) {
                this.serviceType = NO_FB_TIME;
            }
        }
    }

    public String getServiceType() {
        return serviceType;
    }

    public void setServiceType(String serviceType) {
        this.serviceType = serviceType == null ? NO_FB_TIME : serviceType.trim();
    }

    public String getStartTime() {
        return StringUtils.leftPad(startTime, 6, '0');
    }

    public void setStartTime(String startTime) {
        String s = startTime == null ? "" : startTime.trim();
        s = StringUtils.leftPad(s, 6, '0');
        this.startTime = s;
    }

    public String getStopTime() {
        return StringUtils.leftPad(stopTime, 6, '0');
    }

    public void setStopTime(String stopTime) {
        String s = stopTime == null ? "" : stopTime.trim();
        s = StringUtils.leftPad(s, 6, '0');
        this.stopTime = s;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getSendTime() {
        return sendTime;
    }

    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }

    public String getJsoncontent() {
        return jsoncontent;
    }

    public void setJsoncontent(String jsoncontent) {
        this.jsoncontent = jsoncontent == null ? null : jsoncontent.trim();
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "TamplateMsgSent [seqnb=" + seqnb + ", status=" + status + ", priority=" + priority + ", clientIp=" + clientIp + ", clientId="
                + clientId + ", openidId=" + openidId + ", templateId=" + templateId + ", sessionId=" + sessionId + ", sequence=" + sequence
                + ", serviceType=" + serviceType + ", startTime=" + startTime + ", stopTime=" + stopTime + ", createTime=" + createTime
                + ", sendTime=" + sendTime + ", jsoncontent=" + jsoncontent + "]";
    }
}